package com.huazai.yangge.lock.support;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * 线程的阻塞和唤醒
 * 1、object的wait和notify必须再synchronized中才可以使用， 且必先await再notify
 * 2、await和signal必须再lock和unlock中使用  且必先await再signal
 * 3、LockSupport.unpark和LockSupport.park没有选后， 注意，只能有一个park
 *
 * @author huazai
 * @date 2024/2/23
 */
public class LockSupportDemo {
    public static void main(String[] args) throws InterruptedException {
        //final Thread thread = new Thread(() -> {
        //    System.out.println("in ");
        //    LockSupport.park();
        //    System.out.println("wakeup");
        //}, "t1");
        //thread.start();
        //
        //TimeUnit.MILLISECONDS.sleep(200);
        //
        //new Thread(() -> {
        //    LockSupport.unpark(thread);
        //    System.out.println("send wakeup");
        //}, "t1").start();

        final Thread thread = new Thread(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("in ");
            LockSupport.park();
            System.out.println("wakeup");
        }, "t1");
        thread.start();

        new Thread(() -> {
            LockSupport.unpark(thread);
            System.out.println("send wakeup");
        }, "t1").start();
    }
}
